package client_gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;

import parameters.RLM_Parameters;

import utils.ImageCreator;

/**
 * Klasa predstavlja splash screen.
 * 
 * @author UrosKrkic & Radivoje Popovic
 */
public class SplashScreenDialog extends JDialog {

	
	private ClientWindow parent;
	
	private JPanel jContentPane = null;
	
	private JScrollPane scrollPane = null;
	
	private JTextPane textPane = null;
	
	private JCheckBox checkBox = null;
	
	private JButton buttonOK = null;
	
	
	
	public SplashScreenDialog(ClientWindow parent) {
		super(parent, "RLM Splash Screen", true);
		
		this.parent = parent;
		
		initialize();
	}
	
	
	/**
	 * Metod vrsi inicijalizaciju dialoga.
	 * @author UrosKrkic & Radivoje Popovic & Martin Jondzic
	 */
	private void initialize() {
		this.setSize(340, 400);
		this.setLocation(parent.getX() + parent.getWidth() / 2 - this.getWidth() / 2, parent.getY() + parent.getHeight() / 2 - this.getHeight() / 2);
		this.setResizable(false);
		this.setContentPane(getJContentPane());
		
		this.setIconImage(parent.getIconImage());
	}
	
	
	// Metode koje inicijalizuju komponente dialoga.
	
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			
			jContentPane.setLayout(new BorderLayout());

			jContentPane.add(getScrollPane(), BorderLayout.CENTER);
			jContentPane.add(getDownPanel(), BorderLayout.SOUTH);
		}
		
		return jContentPane;
	}
	
	
	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTextPane());
			scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		}
		
		return scrollPane;
	}
	
	
	private JTextPane getTextPane() {
		if (textPane == null) {
			textPane = new JTextPane();
			textPane.setEditable(false);
			
			StyledDocument styledDocument = textPane.getStyledDocument();
			
			Style iconStyle = styledDocument.addStyle("icon", null);
			StyleConstants.setAlignment(iconStyle, StyleConstants.ALIGN_JUSTIFIED);
			StyleConstants.setIcon(iconStyle, ImageCreator.createImageIcon(SplashScreenDialog.class, "client_icon.png"));

			Style titleStyle = styledDocument.addStyle("title", null);
			StyleConstants.setAlignment(titleStyle, StyleConstants.ALIGN_CENTER);
			StyleConstants.setBold(titleStyle, true);
			StyleConstants.setFontSize(titleStyle, 18);
			
			Style versionStyle = styledDocument.addStyle("version", null);
			StyleConstants.setAlignment(versionStyle, StyleConstants.ALIGN_CENTER);
			StyleConstants.setBold(versionStyle, true);
			StyleConstants.setFontSize(versionStyle, 16);
			
			Style textStyle = styledDocument.addStyle("text", null);
			StyleConstants.setAlignment(textStyle, StyleConstants.ALIGN_JUSTIFIED);
			StyleConstants.setItalic(textStyle, true);
			StyleConstants.setFontSize(textStyle, 14);
			
			Style napomenaStyle = styledDocument.addStyle("napomena", null);
			StyleConstants.setAlignment(napomenaStyle, StyleConstants.ALIGN_CENTER);
			StyleConstants.setBold(napomenaStyle, true);
			StyleConstants.setFontSize(napomenaStyle, 14);
			
			Style textNormal = styledDocument.addStyle("normal", null);
			StyleConstants.setAlignment(textNormal, StyleConstants.ALIGN_JUSTIFIED);
			StyleConstants.setFontSize(textNormal, 14);
			
			Style mailStyle = styledDocument.addStyle("mail", null);
			StyleConstants.setAlignment(mailStyle, StyleConstants.ALIGN_CENTER);
			StyleConstants.setBold(mailStyle, true);
			StyleConstants.setFontSize(mailStyle, 12);

			String titleText = "  RAF Live Messenger  ";
			String versionText = "\n\t\tVersion: 2.0";
			
			StringBuffer textBuffer = new StringBuffer();
			
			textBuffer.append("\n\n     Poštovani,\n\n");
			
			textBuffer.append("     Predstavljamo Vam drugu verziju RAF Live Messengera.");
			textBuffer.append(" Sve primedbe i sugestije, koje nam budete uputili, doprineće većoj funkcionalnisti sledeće verzije RLM Messengera.");
			textBuffer.append("\nHvala Vam unapred i nadamo se da ćete uživati uz instant messaging koji Vam pruža RLM!\n\n");
			
			String t1 = "     Zbog interne greške u Swing-u, može se desiti da se stablo sa korisnicima ne prikaže korektno . U tom slučaju, kliknuti na dugme ";
			String t2 = ", i lista korisnika će se osvežiti.\n\n";
			
			String s = "Svaku kritiku ili komentar u vezi sa RLM-om možete poslati na:\n";
			String m1 = "\turos.krkic@gmail.com\n";
			String m2 = "\tpoposhar@gmail.com\n\n";
			
			String t3 = "RLM Team 2008.\n";
			String t4 = "Računarski fakultet\n";
			String t5 = "Beograd\n";
			
			try {
				styledDocument.insertString(styledDocument.getLength(), " ", iconStyle);
				styledDocument.insertString(styledDocument.getLength(), titleText, titleStyle);
				styledDocument.insertString(styledDocument.getLength(), " ", iconStyle);
				styledDocument.insertString(styledDocument.getLength(), versionText, versionStyle);
				styledDocument.insertString(styledDocument.getLength(), textBuffer.toString(), textStyle);
				styledDocument.insertString(styledDocument.getLength(), "NAPOMENA:\n", napomenaStyle);
				styledDocument.insertString(styledDocument.getLength(), t1, textNormal);
				textPane.insertIcon(ImageCreator.createImageIcon(SplashScreenDialog.class, "/actions/images/refresh.png"));
				styledDocument.insertString(styledDocument.getLength(), t2, textNormal);
				
				styledDocument.insertString(styledDocument.getLength(), s, textNormal);
				styledDocument.insertString(styledDocument.getLength(), m1, mailStyle);
				styledDocument.insertString(styledDocument.getLength(), m2, mailStyle);
				
				styledDocument.insertString(styledDocument.getLength(), t3, textStyle);
				styledDocument.insertString(styledDocument.getLength(), t4, textStyle);
				styledDocument.insertString(styledDocument.getLength(), t5, textStyle);
				
				textPane.setCaretPosition(0);
			} catch (BadLocationException e) {
				e.printStackTrace();
			}
		}
		
		return textPane;
	}
	
	
	
	private JCheckBox getCheckBox() {
		if (checkBox == null) {
			checkBox = new JCheckBox("Do not show again");
			
//			checkBox.setSelected(parent.getScreenFlag());
			checkBox.setSelected(RLM_Parameters.isSplashScreen());
		}
		
		return checkBox;
	}
	
	
	private JButton getOKButton() {
		if (buttonOK == null) {
			buttonOK = new JButton("OK");
			buttonOK.setPreferredSize(new Dimension(70,25));
			buttonOK.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
//					parent.setScreenFlag(checkBox.isSelected());
					RLM_Parameters.setSplashScreen(checkBox.isSelected());
					SplashScreenDialog.this.dispose();
				}
			});
		}
		
		return buttonOK;
	}
	
	
	private JPanel downPanel = null;
	
	private JPanel getDownPanel() {
		if (downPanel == null) {
			downPanel = new JPanel(new BorderLayout());
			downPanel.add(getCheckBox(), BorderLayout.WEST);
			downPanel.add(getOKButton(), BorderLayout.EAST);
		}
		
		return downPanel;
	}
	
}

